package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.EpidemicPreventionMapper;
import com.lnking.lnkingplace.model.dto.epidemicPrevention.EpidemicPreventionExportDto;
import com.lnking.lnkingplace.model.dto.epidemicPrevention.EpidemicPreventionImportDto;
import com.lnking.lnkingplace.model.entity.EpidemicPrevention;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.epidemicPrevention.EpidemicPreventionAddRequest;
import com.lnking.lnkingplace.model.request.epidemicPrevention.EpidemicPreventionUpdateRequest;
import com.lnking.lnkingplace.service.IEpidemicPreventionService;
import com.lnking.starter.export.utils.ExportUtils;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 防疫数据导入 服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-20
 */
@Service
public class EpidemicPreventionServiceImpl extends CustomServiceImpl<EpidemicPreventionMapper, EpidemicPrevention> implements IEpidemicPreventionService {

    @Override
    public void add(EpidemicPreventionAddRequest params) throws Exception {
        EpidemicPrevention entity = new EpidemicPrevention();
        BeanUtils.copyProperties(params, entity);
        entity.setCreateTime(LocalDateTime.now());
        getBaseMapper().insert(entity);
    }

    @Override
    public void update(EpidemicPreventionUpdateRequest params) throws Exception {
        EpidemicPrevention entity = new EpidemicPrevention();
        BeanUtils.copyProperties(params, entity);
        getBaseMapper().updateById(entity);
    }

    @Override
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<EpidemicPreventionImportDto> result =  ImportUtils.importExcel(file, EpidemicPreventionImportDto.class,option);
        result.forEach(r ->{
            EpidemicPrevention info = new EpidemicPrevention();
            BeanUtils.copyProperties(r,info);
            String lastCheckTime = r.getLastCheckTime();
            if(StringUtils.isNotBlank(lastCheckTime)){
                info.setLastCheckTime(LocalDateTimeUtil.stringToLocalDateTime(lastCheckTime, DateFormat.LOCAL_DATE));
            }
            info.setCreateTime(LocalDateTime.now());
            this.baseMapper.insert(info);
        });
    }

    @Override
    public void exportExcel(HttpServletResponse response) throws Exception {
        //todo 先查所有 后续根据条件筛选 数据量大时限制时间范围
        List<EpidemicPreventionExportDto> exportData = this.baseMapper.queryExportData();
        ExportUtils.export(exportData,response, EpidemicPreventionExportDto.class);
    }
}
